import {createSlice,createAsyncThunk} from '@reduxjs/toolkit'
import axios from 'axios'
interface BuycarState{
    isloading:boolean,
    error:string|null,
    buycarproducts:any[],
    orderid:any
}
const initialState:BuycarState={
    isloading:false,
    error:null,
    buycarproducts:[],
    orderid:null
}
export const getBuycar=createAsyncThunk(
    "Buycar/getBuycar",
    async (token:string)=>{
        const {data} = await axios.get('http://123.56.149.216:8080/api/shoppingCart',{
            headers:{
                "Authorization":"bearer "+token,
                "x-icode": "69ACF3816BF5D32D"
            }
        })
        return data.shoppingCartItems;
    }
)
export const addBuycar=createAsyncThunk(
    "Buycar/addBuycar",
    async (params:{productid:string,token:string})=>{
        const {data}=await axios.post('http://123.56.149.216:8080/api/shoppingCart/items/',{
            "touristRouteId":params.productid
        },{
            headers:{
                "Authorization":"bearer "+params.token,
                "x-icode": "69ACF3816BF5D32D"
            }
        })
        return data.shoppingCartItems;
    }
)
export const deleteBuycar=createAsyncThunk(
    "Buycar/deleteBuycar",
    async (params:{productid:string[],token:string})=>{
        var param=params.productid.length===1 ? params.productid : '('+params.productid.join(',')+')';
         return await axios.delete('http://123.56.149.216:8080/api/shoppingCart/items/'+param,{
            headers:{
                "Authorization":"bearer "+params.token,
                "x-icode": "69ACF3816BF5D32D"
            }
        })
    }
)
export const checkout=createAsyncThunk(
    "Buycar/checkout",
    async (token:string)=>{
        const {data} = await axios.post('http://123.56.149.216:8080/api/shoppingCart/checkout',null,{
            headers:{
                "Authorization":"bearer "+token,
                "x-icode": "69ACF3816BF5D32D"
            }
        })
        return data.id;
    }
)
export const BuycarSlice=createSlice({
    name:'Buycar',
    initialState,
    reducers:{},
    extraReducers:{
        [getBuycar.pending.type]:(state)=>{
            state.isloading=true;
        },
        [getBuycar.fulfilled.type]:(state,action)=>{
            state.isloading=false;
            state.buycarproducts=action.payload;
        },
        [getBuycar.rejected.type]:(state,action)=>{
            state.isloading=false;
            state.error=action.error.message;
        },

        [addBuycar.pending.type]:(state)=>{
            state.isloading=true;
        },
        [addBuycar.fulfilled.type]:(state,action)=>{
            state.isloading=false;
            state.buycarproducts=action.payload;
        },
        [addBuycar.rejected.type]:(state,action)=>{
            state.isloading=false;
            state.error=action.error.message;
        },

        [deleteBuycar.pending.type]:(state)=>{
            state.isloading=true;
        },
        [deleteBuycar.fulfilled.type]:(state)=>{
            state.isloading=false;
            state.buycarproducts=[];
        },
        [deleteBuycar.rejected.type]:(state,action)=>{
            state.isloading=false;
            state.error=action.error.message;
        },
        [checkout.fulfilled.type]:(state)=>{
            state.isloading=true;
        },
        [checkout.fulfilled.type]:(state,action)=>{
            state.isloading=false;
            state.buycarproducts=[];
            state.orderid=action.payload;
        },
        [checkout.rejected.type]:(state,action)=>{
            state.isloading=false;
            state.error=action.error.message;
        },
    }
})